home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / gcc-2.95.3-3 / info / gcc.info-11 < prev    next >
Encoding:
GNU Info File  |  2001-07-15  |  48.4 KB  |  1,066 lines

  1. This is Info file gcc.info, produced by Makeinfo version 1.68 from the
  2. input file ./gcc.texi.
  3.  
  4. INFO-DIR-SECTION Programming
  5. START-INFO-DIR-ENTRY
  6. * gcc: (gcc).                  The GNU Compiler Collection.
  7. END-INFO-DIR-ENTRY
  8.    This file documents the use and the internals of the GNU compiler.
  9.  
  10.    Published by the Free Software Foundation 59 Temple Place - Suite 330
  11. Boston, MA 02111-1307 USA
  12.  
  13.    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  14. 1999, 2000 Free Software Foundation, Inc.
  15.  
  16.    Permission is granted to make and distribute verbatim copies of this
  17. manual provided the copyright notice and this permission notice are
  18. preserved on all copies.
  19.  
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the sections entitled "GNU General Public License" and "Funding
  23. for Free Software" are included exactly as in the original, and
  24. provided that the entire resulting derived work is distributed under
  25. the terms of a permission notice identical to this one.
  26.  
  27.    Permission is granted to copy and distribute translations of this
  28. manual into another language, under the above conditions for modified
  29. versions, except that the sections entitled "GNU General Public
  30. License" and "Funding for Free Software", and this permission notice,
  31. may be included in translations approved by the Free Software Foundation
  32. instead of in the original English.
  33.  
  34. 
  35. File: gcc.info,  Node: Type Attributes,  Next: Alignment,  Prev: Variable Attributes,  Up: C Extensions
  36.  
  37. Specifying Attributes of Types
  38. ==============================
  39.  
  40.    The keyword `__attribute__' allows you to specify special attributes
  41. of `struct' and `union' types when you define such types.  This keyword
  42. is followed by an attribute specification inside double parentheses.
  43. Three attributes are currently defined for types: `aligned', `packed',
  44. and `transparent_union'.  Other attributes are defined for functions
  45. (*note Function Attributes::.) and for variables (*note Variable
  46. Attributes::.).
  47.  
  48.    You may also specify any one of these attributes with `__' preceding
  49. and following its keyword.  This allows you to use these attributes in
  50. header files without being concerned about a possible macro of the same
  51. name.  For example, you may use `__aligned__' instead of `aligned'.
  52.  
  53.    You may specify the `aligned' and `transparent_union' attributes
  54. either in a `typedef' declaration or just past the closing curly brace
  55. of a complete enum, struct or union type *definition* and the `packed'
  56. attribute only past the closing brace of a definition.
  57.  
  58.    You may also specify attributes between the enum, struct or union
  59. tag and the name of the type rather than after the closing brace.
  60.  
  61. `aligned (ALIGNMENT)'
  62.      This attribute specifies a minimum alignment (in bytes) for
  63.      variables of the specified type.  For example, the declarations:
  64.  
  65.           struct S { short f[3]; } __attribute__ ((aligned (8)));
  66.           typedef int more_aligned_int __attribute__ ((aligned (8)));
  67.  
  68.      force the compiler to insure (as far as it can) that each variable
  69.      whose type is `struct S' or `more_aligned_int' will be allocated
  70.      and aligned *at least* on a 8-byte boundary.  On a Sparc, having
  71.      all variables of type `struct S' aligned to 8-byte boundaries
  72.      allows the compiler to use the `ldd' and `std' (doubleword load and
  73.      store) instructions when copying one variable of type `struct S' to
  74.      another, thus improving run-time efficiency.
  75.  
  76.      Note that the alignment of any given `struct' or `union' type is
  77.      required by the ANSI C standard to be at least a perfect multiple
  78.      of the lowest common multiple of the alignments of all of the
  79.      members of the `struct' or `union' in question.  This means that
  80.      you *can* effectively adjust the alignment of a `struct' or `union'
  81.      type by attaching an `aligned' attribute to any one of the members
  82.      of such a type, but the notation illustrated in the example above
  83.      is a more obvious, intuitive, and readable way to request the
  84.      compiler to adjust the alignment of an entire `struct' or `union'
  85.      type.
  86.  
  87.      As in the preceding example, you can explicitly specify the
  88.      alignment (in bytes) that you wish the compiler to use for a given
  89.      `struct' or `union' type.  Alternatively, you can leave out the
  90.      alignment factor and just ask the compiler to align a type to the
  91.      maximum useful alignment for the target machine you are compiling
  92.      for.  For example, you could write:
  93.  
  94.           struct S { short f[3]; } __attribute__ ((aligned));
  95.  
  96.      Whenever you leave out the alignment factor in an `aligned'
  97.      attribute specification, the compiler automatically sets the
  98.      alignment for the type to the largest alignment which is ever used
  99.      for any data type on the target machine you are compiling for.
  100.      Doing this can often make copy operations more efficient, because
  101.      the compiler can use whatever instructions copy the biggest chunks
  102.      of memory when performing copies to or from the variables which
  103.      have types that you have aligned this way.
  104.  
  105.      In the example above, if the size of each `short' is 2 bytes, then
  106.      the size of the entire `struct S' type is 6 bytes.  The smallest
  107.      power of two which is greater than or equal to that is 8, so the
  108.      compiler sets the alignment for the entire `struct S' type to 8
  109.      bytes.
  110.  
  111.      Note that although you can ask the compiler to select a
  112.      time-efficient alignment for a given type and then declare only
  113.      individual stand-alone objects of that type, the compiler's
  114.      ability to select a time-efficient alignment is primarily useful
  115.      only when you plan to create arrays of variables having the
  116.      relevant (efficiently aligned) type.  If you declare or use arrays
  117.      of variables of an efficiently-aligned type, then it is likely
  118.      that your program will also be doing pointer arithmetic (or
  119.      subscripting, which amounts to the same thing) on pointers to the
  120.      relevant type, and the code that the compiler generates for these
  121.      pointer arithmetic operations will often be more efficient for
  122.      efficiently-aligned types than for other types.
  123.  
  124.      The `aligned' attribute can only increase the alignment; but you
  125.      can decrease it by specifying `packed' as well.  See below.
  126.  
  127.      Note that the effectiveness of `aligned' attributes may be limited
  128.      by inherent limitations in your linker.  On many systems, the
  129.      linker is only able to arrange for variables to be aligned up to a
  130.      certain maximum alignment.  (For some linkers, the maximum
  131.      supported alignment may be very very small.)  If your linker is
  132.      only able to align variables up to a maximum of 8 byte alignment,
  133.      then specifying `aligned(16)' in an `__attribute__' will still
  134.      only provide you with 8 byte alignment.  See your linker
  135.      documentation for further information.
  136.  
  137. `packed'
  138.      This attribute, attached to an `enum', `struct', or `union' type
  139.      definition, specified that the minimum required memory be used to
  140.      represent the type.
  141.  
  142.      Specifying this attribute for `struct' and `union' types is
  143.      equivalent to specifying the `packed' attribute on each of the
  144.      structure or union members.  Specifying the `-fshort-enums' flag
  145.      on the line is equivalent to specifying the `packed' attribute on
  146.      all `enum' definitions.
  147.  
  148.      You may only specify this attribute after a closing curly brace on
  149.      an `enum' definition, not in a `typedef' declaration, unless that
  150.      declaration also contains the definition of the `enum'.
  151.  
  152. `transparent_union'
  153.      This attribute, attached to a `union' type definition, indicates
  154.      that any function parameter having that union type causes calls to
  155.      that function to be treated in a special way.
  156.  
  157.      First, the argument corresponding to a transparent union type can
  158.      be of any type in the union; no cast is required.  Also, if the
  159.      union contains a pointer type, the corresponding argument can be a
  160.      null pointer constant or a void pointer expression; and if the
  161.      union contains a void pointer type, the corresponding argument can
  162.      be any pointer expression.  If the union member type is a pointer,
  163.      qualifiers like `const' on the referenced type must be respected,
  164.      just as with normal pointer conversions.
  165.  
  166.      Second, the argument is passed to the function using the calling
  167.      conventions of first member of the transparent union, not the
  168.      calling conventions of the union itself.  All members of the union
  169.      must have the same machine representation; this is necessary for
  170.      this argument passing to work properly.
  171.  
  172.      Transparent unions are designed for library functions that have
  173.      multiple interfaces for compatibility reasons.  For example,
  174.      suppose the `wait' function must accept either a value of type
  175.      `int *' to comply with Posix, or a value of type `union wait *' to
  176.      comply with the 4.1BSD interface.  If `wait''s parameter were
  177.      `void *', `wait' would accept both kinds of arguments, but it
  178.      would also accept any other pointer type and this would make
  179.      argument type checking less useful.  Instead, `<sys/wait.h>' might
  180.      define the interface as follows:
  181.  
  182.           typedef union
  183.             {
  184.               int *__ip;
  185.               union wait *__up;
  186.             } wait_status_ptr_t __attribute__ ((__transparent_union__));
  187.           
  188.           pid_t wait (wait_status_ptr_t);
  189.  
  190.      This interface allows either `int *' or `union wait *' arguments
  191.      to be passed, using the `int *' calling convention.  The program
  192.      can call `wait' with arguments of either type:
  193.  
  194.           int w1 () { int w; return wait (&w); }
  195.           int w2 () { union wait w; return wait (&w); }
  196.  
  197.      With this interface, `wait''s implementation might look like this:
  198.  
  199.           pid_t wait (wait_status_ptr_t p)
  200.           {
  201.             return waitpid (-1, p.__ip, 0);
  202.           }
  203.  
  204. `unused'
  205.      When attached to a type (including a `union' or a `struct'), this
  206.      attribute means that variables of that type are meant to appear
  207.      possibly unused.  GNU CC will not produce a warning for any
  208.      variables of that type, even if the variable appears to do
  209.      nothing.  This is often the case with lock or thread classes,
  210.      which are usually defined and then not referenced, but contain
  211.      constructors and destructors that have nontrivial bookkeeping
  212.      functions.
  213.  
  214.    To specify multiple attributes, separate them by commas within the
  215. double parentheses: for example, `__attribute__ ((aligned (16),
  216. packed))'.
  217.  
  218. 
  219. File: gcc.info,  Node: Inline,  Next: Extended Asm,  Prev: Alignment,  Up: C Extensions
  220.  
  221. An Inline Function is As Fast As a Macro
  222. ========================================
  223.  
  224.    By declaring a function `inline', you can direct GNU CC to integrate
  225. that function's code into the code for its callers.  This makes
  226. execution faster by eliminating the function-call overhead; in
  227. addition, if any of the actual argument values are constant, their known
  228. values may permit simplifications at compile time so that not all of the
  229. inline function's code needs to be included.  The effect on code size is
  230. less predictable; object code may be larger or smaller with function
  231. inlining, depending on the particular case.  Inlining of functions is an
  232. optimization and it really "works" only in optimizing compilation.  If
  233. you don't use `-O', no function is really inline.
  234.  
  235.    To declare a function inline, use the `inline' keyword in its
  236. declaration, like this:
  237.  
  238.      inline int
  239.      inc (int *a)
  240.      {
  241.        (*a)++;
  242.      }
  243.  
  244.    (If you are writing a header file to be included in ANSI C programs,
  245. write `__inline__' instead of `inline'.  *Note Alternate Keywords::.)
  246. You can also make all "simple enough" functions inline with the option
  247. `-finline-functions'.
  248.  
  249.    Note that certain usages in a function definition can make it
  250. unsuitable for inline substitution.  Among these usages are: use of
  251. varargs, use of alloca, use of variable sized data types (*note
  252. Variable Length::.), use of computed goto (*note Labels as Values::.),
  253. use of nonlocal goto, and nested functions (*note Nested Functions::.).
  254. Using `-Winline' will warn when a function marked `inline' could not
  255. be substituted, and will give the reason for the failure.
  256.  
  257.    Note that in C and Objective C, unlike C++, the `inline' keyword
  258. does not affect the linkage of the function.
  259.  
  260.    GNU CC automatically inlines member functions defined within the
  261. class body of C++ programs even if they are not explicitly declared
  262. `inline'.  (You can override this with `-fno-default-inline'; *note
  263. Options Controlling C++ Dialect: C++ Dialect Options..)
  264.  
  265.    When a function is both inline and `static', if all calls to the
  266. function are integrated into the caller, and the function's address is
  267. never used, then the function's own assembler code is never referenced.
  268. In this case, GNU CC does not actually output assembler code for the
  269. function, unless you specify the option `-fkeep-inline-functions'.
  270. Some calls cannot be integrated for various reasons (in particular,
  271. calls that precede the function's definition cannot be integrated, and
  272. neither can recursive calls within the definition).  If there is a
  273. nonintegrated call, then the function is compiled to assembler code as
  274. usual.  The function must also be compiled as usual if the program
  275. refers to its address, because that can't be inlined.
  276.  
  277.    When an inline function is not `static', then the compiler must
  278. assume that there may be calls from other source files; since a global
  279. symbol can be defined only once in any program, the function must not
  280. be defined in the other source files, so the calls therein cannot be
  281. integrated.  Therefore, a non-`static' inline function is always
  282. compiled on its own in the usual fashion.
  283.  
  284.    If you specify both `inline' and `extern' in the function
  285. definition, then the definition is used only for inlining.  In no case
  286. is the function compiled on its own, not even if you refer to its
  287. address explicitly.  Such an address becomes an external reference, as
  288. if you had only declared the function, and had not defined it.
  289.  
  290.    This combination of `inline' and `extern' has almost the effect of a
  291. macro.  The way to use it is to put a function definition in a header
  292. file with these keywords, and put another copy of the definition
  293. (lacking `inline' and `extern') in a library file.  The definition in
  294. the header file will cause most calls to the function to be inlined.
  295. If any uses of the function remain, they will refer to the single copy
  296. in the library.
  297.  
  298.    GNU C does not inline any functions when not optimizing.  It is not
  299. clear whether it is better to inline or not, in this case, but we found
  300. that a correct implementation when not optimizing was difficult.  So we
  301. did the easy thing, and turned it off.
  302.  
  303. 
  304. File: gcc.info,  Node: Extended Asm,  Next: Asm Labels,  Prev: Inline,  Up: C Extensions
  305.  
  306. Assembler Instructions with C Expression Operands
  307. =================================================
  308.  
  309.    In an assembler instruction using `asm', you can specify the
  310. operands of the instruction using C expressions.  This means you need
  311. not guess which registers or memory locations will contain the data you
  312. want to use.
  313.  
  314.    You must specify an assembler instruction template much like what
  315. appears in a machine description, plus an operand constraint string for
  316. each operand.
  317.  
  318.    For example, here is how to use the 68881's `fsinx' instruction:
  319.  
  320.      asm ("fsinx %1,%0" : "=f" (result) : "f" (angle));
  321.  
  322. Here `angle' is the C expression for the input operand while `result'
  323. is that of the output operand.  Each has `"f"' as its operand
  324. constraint, saying that a floating point register is required.  The `='
  325. in `=f' indicates that the operand is an output; all output operands'
  326. constraints must use `='.  The constraints use the same language used
  327. in the machine description (*note Constraints::.).
  328.  
  329.    Each operand is described by an operand-constraint string followed by
  330. the C expression in parentheses.  A colon separates the assembler
  331. template from the first output operand and another separates the last
  332. output operand from the first input, if any.  Commas separate the
  333. operands within each group.  The total number of operands is limited to
  334. ten or to the maximum number of operands in any instruction pattern in
  335. the machine description, whichever is greater.
  336.  
  337.    If there are no output operands but there are input operands, you
  338. must place two consecutive colons surrounding the place where the output
  339. operands would go.
  340.  
  341.    Output operand expressions must be lvalues; the compiler can check
  342. this.  The input operands need not be lvalues.  The compiler cannot
  343. check whether the operands have data types that are reasonable for the
  344. instruction being executed.  It does not parse the assembler instruction
  345. template and does not know what it means or even whether it is valid
  346. assembler input.  The extended `asm' feature is most often used for
  347. machine instructions the compiler itself does not know exist.  If the
  348. output expression cannot be directly addressed (for example, it is a
  349. bit field), your constraint must allow a register.  In that case, GNU CC
  350. will use the register as the output of the `asm', and then store that
  351. register into the output.
  352.  
  353.    The ordinary output operands must be write-only; GNU CC will assume
  354. that the values in these operands before the instruction are dead and
  355. need not be generated.  Extended asm supports input-output or read-write
  356. operands.  Use the constraint character `+' to indicate such an operand
  357. and list it with the output operands.
  358.  
  359.    When the constraints for the read-write operand (or the operand in
  360. which only some of the bits are to be changed) allows a register, you
  361. may, as an alternative, logically split its function into two separate
  362. operands, one input operand and one write-only output operand.  The
  363. connection between them is expressed by constraints which say they need
  364. to be in the same location when the instruction executes.  You can use
  365. the same C expression for both operands, or different expressions.  For
  366. example, here we write the (fictitious) `combine' instruction with
  367. `bar' as its read-only source operand and `foo' as its read-write
  368. destination:
  369.  
  370.      asm ("combine %2,%0" : "=r" (foo) : "0" (foo), "g" (bar));
  371.  
  372. The constraint `"0"' for operand 1 says that it must occupy the same
  373. location as operand 0.  A digit in constraint is allowed only in an
  374. input operand and it must refer to an output operand.
  375.  
  376.    Only a digit in the constraint can guarantee that one operand will
  377. be in the same place as another.  The mere fact that `foo' is the value
  378. of both operands is not enough to guarantee that they will be in the
  379. same place in the generated assembler code.  The following would not
  380. work reliably:
  381.  
  382.      asm ("combine %2,%0" : "=r" (foo) : "r" (foo), "g" (bar));
  383.  
  384.    Various optimizations or reloading could cause operands 0 and 1 to
  385. be in different registers; GNU CC knows no reason not to do so.  For
  386. example, the compiler might find a copy of the value of `foo' in one
  387. register and use it for operand 1, but generate the output operand 0 in
  388. a different register (copying it afterward to `foo''s own address).  Of
  389. course, since the register for operand 1 is not even mentioned in the
  390. assembler code, the result will not work, but GNU CC can't tell that.
  391.  
  392.    Some instructions clobber specific hard registers.  To describe this,
  393. write a third colon after the input operands, followed by the names of
  394. the clobbered hard registers (given as strings).  Here is a realistic
  395. example for the VAX:
  396.  
  397.      asm volatile ("movc3 %0,%1,%2"
  398.                    : /* no outputs */
  399.                    : "g" (from), "g" (to), "g" (count)
  400.                    : "r0", "r1", "r2", "r3", "r4", "r5");
  401.  
  402.    It is an error for a clobber description to overlap an input or
  403. output operand (for example, an operand describing a register class
  404. with one member, mentioned in the clobber list).  Most notably, it is
  405. invalid to describe that an input operand is modified, but unused as
  406. output.  It has to be specified as an input and output operand anyway.
  407. Note that if there are only unused output operands, you will then also
  408. need to specify `volatile' for the `asm' construct, as described below.
  409.  
  410.    If you refer to a particular hardware register from the assembler
  411. code, you will probably have to list the register after the third colon
  412. to tell the compiler the register's value is modified.  In some
  413. assemblers, the register names begin with `%'; to produce one `%' in the
  414. assembler code, you must write `%%' in the input.
  415.  
  416.    If your assembler instruction can alter the condition code register,
  417. add `cc' to the list of clobbered registers.  GNU CC on some machines
  418. represents the condition codes as a specific hardware register; `cc'
  419. serves to name this register.  On other machines, the condition code is
  420. handled differently, and specifying `cc' has no effect.  But it is
  421. valid no matter what the machine.
  422.  
  423.    If your assembler instruction modifies memory in an unpredictable
  424. fashion, add `memory' to the list of clobbered registers.  This will
  425. cause GNU CC to not keep memory values cached in registers across the
  426. assembler instruction.
  427.  
  428.    You can put multiple assembler instructions together in a single
  429. `asm' template, separated either with newlines (written as `\n') or
  430. with semicolons if the assembler allows such semicolons.  The GNU
  431. assembler allows semicolons and most Unix assemblers seem to do so.
  432. The input operands are guaranteed not to use any of the clobbered
  433. registers, and neither will the output operands' addresses, so you can
  434. read and write the clobbered registers as many times as you like.  Here
  435. is an example of multiple instructions in a template; it assumes the
  436. subroutine `_foo' accepts arguments in registers 9 and 10:
  437.  
  438.      asm ("movl %0,r9;movl %1,r10;call _foo"
  439.           : /* no outputs */
  440.           : "g" (from), "g" (to)
  441.           : "r9", "r10");
  442.  
  443.    Unless an output operand has the `&' constraint modifier, GNU CC may
  444. allocate it in the same register as an unrelated input operand, on the
  445. assumption the inputs are consumed before the outputs are produced.
  446. This assumption may be false if the assembler code actually consists of
  447. more than one instruction.  In such a case, use `&' for each output
  448. operand that may not overlap an input.  *Note Modifiers::.
  449.  
  450.    If you want to test the condition code produced by an assembler
  451. instruction, you must include a branch and a label in the `asm'
  452. construct, as follows:
  453.  
  454.      asm ("clr %0;frob %1;beq 0f;mov #1,%0;0:"
  455.           : "g" (result)
  456.           : "g" (input));
  457.  
  458. This assumes your assembler supports local labels, as the GNU assembler
  459. and most Unix assemblers do.
  460.  
  461.    Speaking of labels, jumps from one `asm' to another are not
  462. supported.  The compiler's optimizers do not know about these jumps, and
  463. therefore they cannot take account of them when deciding how to
  464. optimize.
  465.  
  466.    Usually the most convenient way to use these `asm' instructions is to
  467. encapsulate them in macros that look like functions.  For example,
  468.  
  469.      #define sin(x)       \
  470.      ({ double __value, __arg = (x);   \
  471.         asm ("fsinx %1,%0": "=f" (__value): "f" (__arg));  \
  472.         __value; })
  473.  
  474. Here the variable `__arg' is used to make sure that the instruction
  475. operates on a proper `double' value, and to accept only those arguments
  476. `x' which can convert automatically to a `double'.
  477.  
  478.    Another way to make sure the instruction operates on the correct data
  479. type is to use a cast in the `asm'.  This is different from using a
  480. variable `__arg' in that it converts more different types.  For
  481. example, if the desired type were `int', casting the argument to `int'
  482. would accept a pointer with no complaint, while assigning the argument
  483. to an `int' variable named `__arg' would warn about using a pointer
  484. unless the caller explicitly casts it.
  485.  
  486.    If an `asm' has output operands, GNU CC assumes for optimization
  487. purposes the instruction has no side effects except to change the output
  488. operands.  This does not mean instructions with a side effect cannot be
  489. used, but you must be careful, because the compiler may eliminate them
  490. if the output operands aren't used, or move them out of loops, or
  491. replace two with one if they constitute a common subexpression.  Also,
  492. if your instruction does have a side effect on a variable that otherwise
  493. appears not to change, the old value of the variable may be reused later
  494. if it happens to be found in a register.
  495.  
  496.    You can prevent an `asm' instruction from being deleted, moved
  497. significantly, or combined, by writing the keyword `volatile' after the
  498. `asm'.  For example:
  499.  
  500.      #define get_and_set_priority(new)  \
  501.      ({ int __old; \
  502.         asm volatile ("get_and_set_priority %0, %1": "=g" (__old) : "g" (new)); \
  503.         __old; })
  504.  
  505. If you write an `asm' instruction with no outputs, GNU CC will know the
  506. instruction has side-effects and will not delete the instruction or
  507. move it outside of loops.  If the side-effects of your instruction are
  508. not purely external, but will affect variables in your program in ways
  509. other than reading the inputs and clobbering the specified registers or
  510. memory, you should write the `volatile' keyword to prevent future
  511. versions of GNU CC from moving the instruction around within a core
  512. region.
  513.  
  514.    An `asm' instruction without any operands or clobbers (and "old
  515. style" `asm') will not be deleted or moved significantly, regardless,
  516. unless it is unreachable, the same wasy as if you had written a
  517. `volatile' keyword.
  518.  
  519.    Note that even a volatile `asm' instruction can be moved in ways
  520. that appear insignificant to the compiler, such as across jump
  521. instructions.  You can't expect a sequence of volatile `asm'
  522. instructions to remain perfectly consecutive.  If you want consecutive
  523. output, use a single `asm'.
  524.  
  525.    It is a natural idea to look for a way to give access to the
  526. condition code left by the assembler instruction.  However, when we
  527. attempted to implement this, we found no way to make it work reliably.
  528. The problem is that output operands might need reloading, which would
  529. result in additional following "store" instructions.  On most machines,
  530. these instructions would alter the condition code before there was time
  531. to test it.  This problem doesn't arise for ordinary "test" and
  532. "compare" instructions because they don't have any output operands.
  533.  
  534.    If you are writing a header file that should be includable in ANSI C
  535. programs, write `__asm__' instead of `asm'.  *Note Alternate Keywords::.
  536.  
  537. i386 floating point asm operands
  538. --------------------------------
  539.  
  540.    There are several rules on the usage of stack-like regs in
  541. asm_operands insns.  These rules apply only to the operands that are
  542. stack-like regs:
  543.  
  544.   1. Given a set of input regs that die in an asm_operands, it is
  545.      necessary to know which are implicitly popped by the asm, and
  546.      which must be explicitly popped by gcc.
  547.  
  548.      An input reg that is implicitly popped by the asm must be
  549.      explicitly clobbered, unless it is constrained to match an output
  550.      operand.
  551.  
  552.   2. For any input reg that is implicitly popped by an asm, it is
  553.      necessary to know how to adjust the stack to compensate for the
  554.      pop.  If any non-popped input is closer to the top of the
  555.      reg-stack than the implicitly popped reg, it would not be possible
  556.      to know what the stack looked like -- it's not clear how the rest
  557.      of the stack "slides up".
  558.  
  559.      All implicitly popped input regs must be closer to the top of the
  560.      reg-stack than any input that is not implicitly popped.
  561.  
  562.      It is possible that if an input dies in an insn, reload might use
  563.      the input reg for an output reload.  Consider this example:
  564.  
  565.           asm ("foo" : "=t" (a) : "f" (b));
  566.  
  567.      This asm says that input B is not popped by the asm, and that the
  568.      asm pushes a result onto the reg-stack, ie, the stack is one
  569.      deeper after the asm than it was before.  But, it is possible that
  570.      reload will think that it can use the same reg for both the input
  571.      and the output, if input B dies in this insn.
  572.  
  573.      If any input operand uses the `f' constraint, all output reg
  574.      constraints must use the `&' earlyclobber.
  575.  
  576.      The asm above would be written as
  577.  
  578.           asm ("foo" : "=&t" (a) : "f" (b));
  579.  
  580.   3. Some operands need to be in particular places on the stack.  All
  581.      output operands fall in this category -- there is no other way to
  582.      know which regs the outputs appear in unless the user indicates
  583.      this in the constraints.
  584.  
  585.      Output operands must specifically indicate which reg an output
  586.      appears in after an asm.  `=f' is not allowed: the operand
  587.      constraints must select a class with a single reg.
  588.  
  589.   4. Output operands may not be "inserted" between existing stack regs.
  590.      Since no 387 opcode uses a read/write operand, all output operands
  591.      are dead before the asm_operands, and are pushed by the
  592.      asm_operands.  It makes no sense to push anywhere but the top of
  593.      the reg-stack.
  594.  
  595.      Output operands must start at the top of the reg-stack: output
  596.      operands may not "skip" a reg.
  597.  
  598.   5. Some asm statements may need extra stack space for internal
  599.      calculations.  This can be guaranteed by clobbering stack registers
  600.      unrelated to the inputs and outputs.
  601.  
  602.  
  603.    Here are a couple of reasonable asms to want to write.  This asm
  604. takes one input, which is internally popped, and produces two outputs.
  605.  
  606.      asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
  607.  
  608.    This asm takes two inputs, which are popped by the `fyl2xp1' opcode,
  609. and replaces them with one output.  The user must code the `st(1)'
  610. clobber for reg-stack.c to know that `fyl2xp1' pops both inputs.
  611.  
  612.      asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
  613.  
  614. 
  615. File: gcc.info,  Node: Asm Labels,  Next: Explicit Reg Vars,  Prev: Extended Asm,  Up: C Extensions
  616.  
  617. Controlling Names Used in Assembler Code
  618. ========================================
  619.  
  620.    You can specify the name to be used in the assembler code for a C
  621. function or variable by writing the `asm' (or `__asm__') keyword after
  622. the declarator as follows:
  623.  
  624.      int foo asm ("myfoo") = 2;
  625.  
  626. This specifies that the name to be used for the variable `foo' in the
  627. assembler code should be `myfoo' rather than the usual `_foo'.
  628.  
  629.    On systems where an underscore is normally prepended to the name of
  630. a C function or variable, this feature allows you to define names for
  631. the linker that do not start with an underscore.
  632.  
  633.    You cannot use `asm' in this way in a function *definition*; but you
  634. can get the same effect by writing a declaration for the function
  635. before its definition and putting `asm' there, like this:
  636.  
  637.      extern func () asm ("FUNC");
  638.      
  639.      func (x, y)
  640.           int x, y;
  641.      ...
  642.  
  643.    It is up to you to make sure that the assembler names you choose do
  644. not conflict with any other assembler symbols.  Also, you must not use a
  645. register name; that would produce completely invalid assembler code.
  646. GNU CC does not as yet have the ability to store static variables in
  647. registers.  Perhaps that will be added.
  648.  
  649. 
  650. File: gcc.info,  Node: Explicit Reg Vars,  Next: Alternate Keywords,  Prev: Asm Labels,  Up: C Extensions
  651.  
  652. Variables in Specified Registers
  653. ================================
  654.  
  655.    GNU C allows you to put a few global variables into specified
  656. hardware registers.  You can also specify the register in which an
  657. ordinary register variable should be allocated.
  658.  
  659.    * Global register variables reserve registers throughout the program.
  660.      This may be useful in programs such as programming language
  661.      interpreters which have a couple of global variables that are
  662.      accessed very often.
  663.  
  664.    * Local register variables in specific registers do not reserve the
  665.      registers.  The compiler's data flow analysis is capable of
  666.      determining where the specified registers contain live values, and
  667.      where they are available for other uses.  Stores into local
  668.      register variables may be deleted when they appear to be dead
  669.      according to dataflow analysis.  References to local register
  670.      variables may be deleted or moved or simplified.
  671.  
  672.      These local variables are sometimes convenient for use with the
  673.      extended `asm' feature (*note Extended Asm::.), if you want to
  674.      write one output of the assembler instruction directly into a
  675.      particular register.  (This will work provided the register you
  676.      specify fits the constraints specified for that operand in the
  677.      `asm'.)
  678.  
  679. * Menu:
  680.  
  681. * Global Reg Vars::
  682. * Local Reg Vars::
  683.  
  684. 
  685. File: gcc.info,  Node: Global Reg Vars,  Next: Local Reg Vars,  Up: Explicit Reg Vars
  686.  
  687. Defining Global Register Variables
  688. ----------------------------------
  689.  
  690.    You can define a global register variable in GNU C like this:
  691.  
  692.      register int *foo asm ("a5");
  693.  
  694. Here `a5' is the name of the register which should be used.  Choose a
  695. register which is normally saved and restored by function calls on your
  696. machine, so that library routines will not clobber it.
  697.  
  698.    Naturally the register name is cpu-dependent, so you would need to
  699. conditionalize your program according to cpu type.  The register `a5'
  700. would be a good choice on a 68000 for a variable of pointer type.  On
  701. machines with register windows, be sure to choose a "global" register
  702. that is not affected magically by the function call mechanism.
  703.  
  704.    In addition, operating systems on one type of cpu may differ in how
  705. they name the registers; then you would need additional conditionals.
  706. For example, some 68000 operating systems call this register `%a5'.
  707.  
  708.    Eventually there may be a way of asking the compiler to choose a
  709. register automatically, but first we need to figure out how it should
  710. choose and how to enable you to guide the choice.  No solution is
  711. evident.
  712.  
  713.    Defining a global register variable in a certain register reserves
  714. that register entirely for this use, at least within the current
  715. compilation.  The register will not be allocated for any other purpose
  716. in the functions in the current compilation.  The register will not be
  717. saved and restored by these functions.  Stores into this register are
  718. never deleted even if they would appear to be dead, but references may
  719. be deleted or moved or simplified.
  720.  
  721.    It is not safe to access the global register variables from signal
  722. handlers, or from more than one thread of control, because the system
  723. library routines may temporarily use the register for other things
  724. (unless you recompile them specially for the task at hand).
  725.  
  726.    It is not safe for one function that uses a global register variable
  727. to call another such function `foo' by way of a third function `lose'
  728. that was compiled without knowledge of this variable (i.e. in a
  729. different source file in which the variable wasn't declared).  This is
  730. because `lose' might save the register and put some other value there.
  731. For example, you can't expect a global register variable to be
  732. available in the comparison-function that you pass to `qsort', since
  733. `qsort' might have put something else in that register.  (If you are
  734. prepared to recompile `qsort' with the same global register variable,
  735. you can solve this problem.)
  736.  
  737.    If you want to recompile `qsort' or other source files which do not
  738. actually use your global register variable, so that they will not use
  739. that register for any other purpose, then it suffices to specify the
  740. compiler option `-ffixed-REG'.  You need not actually add a global
  741. register declaration to their source code.
  742.  
  743.    A function which can alter the value of a global register variable
  744. cannot safely be called from a function compiled without this variable,
  745. because it could clobber the value the caller expects to find there on
  746. return.  Therefore, the function which is the entry point into the part
  747. of the program that uses the global register variable must explicitly
  748. save and restore the value which belongs to its caller.
  749.  
  750.    On most machines, `longjmp' will restore to each global register
  751. variable the value it had at the time of the `setjmp'.  On some
  752. machines, however, `longjmp' will not change the value of global
  753. register variables.  To be portable, the function that called `setjmp'
  754. should make other arrangements to save the values of the global register
  755. variables, and to restore them in a `longjmp'.  This way, the same
  756. thing will happen regardless of what `longjmp' does.
  757.  
  758.    All global register variable declarations must precede all function
  759. definitions.  If such a declaration could appear after function
  760. definitions, the declaration would be too late to prevent the register
  761. from being used for other purposes in the preceding functions.
  762.  
  763.    Global register variables may not have initial values, because an
  764. executable file has no means to supply initial contents for a register.
  765.  
  766.    On the Sparc, there are reports that g3 ... g7 are suitable
  767. registers, but certain library functions, such as `getwd', as well as
  768. the subroutines for division and remainder, modify g3 and g4.  g1 and
  769. g2 are local temporaries.
  770.  
  771.    On the 68000, a2 ... a5 should be suitable, as should d2 ... d7.  Of
  772. course, it will not do to use more than a few of those.
  773.  
  774. 
  775. File: gcc.info,  Node: Local Reg Vars,  Prev: Global Reg Vars,  Up: Explicit Reg Vars
  776.  
  777. Specifying Registers for Local Variables
  778. ----------------------------------------
  779.  
  780.    You can define a local register variable with a specified register
  781. like this:
  782.  
  783.      register int *foo asm ("a5");
  784.  
  785. Here `a5' is the name of the register which should be used.  Note that
  786. this is the same syntax used for defining global register variables,
  787. but for a local variable it would appear within a function.
  788.  
  789.    Naturally the register name is cpu-dependent, but this is not a
  790. problem, since specific registers are most often useful with explicit
  791. assembler instructions (*note Extended Asm::.).  Both of these things
  792. generally require that you conditionalize your program according to cpu
  793. type.
  794.  
  795.    In addition, operating systems on one type of cpu may differ in how
  796. they name the registers; then you would need additional conditionals.
  797. For example, some 68000 operating systems call this register `%a5'.
  798.  
  799.    Defining such a register variable does not reserve the register; it
  800. remains available for other uses in places where flow control determines
  801. the variable's value is not live.  However, these registers are made
  802. unavailable for use in the reload pass; excessive use of this feature
  803. leaves the compiler too few available registers to compile certain
  804. functions.
  805.  
  806.    This option does not guarantee that GNU CC will generate code that
  807. has this variable in the register you specify at all times.  You may not
  808. code an explicit reference to this register in an `asm' statement and
  809. assume it will always refer to this variable.
  810.  
  811.    Stores into local register variables may be deleted when they appear
  812. to be dead according to dataflow analysis.  References to local
  813. register variables may be deleted or moved or simplified.
  814.  
  815. 
  816. File: gcc.info,  Node: Alternate Keywords,  Next: Incomplete Enums,  Prev: Explicit Reg Vars,  Up: C Extensions
  817.  
  818. Alternate Keywords
  819. ==================
  820.  
  821.    The option `-traditional' disables certain keywords; `-ansi'
  822. disables certain others.  This causes trouble when you want to use GNU C
  823. extensions, or ANSI C features, in a general-purpose header file that
  824. should be usable by all programs, including ANSI C programs and
  825. traditional ones.  The keywords `asm', `typeof' and `inline' cannot be
  826. used since they won't work in a program compiled with `-ansi', while
  827. the keywords `const', `volatile', `signed', `typeof' and `inline' won't
  828. work in a program compiled with `-traditional'.
  829.  
  830.    The way to solve these problems is to put `__' at the beginning and
  831. end of each problematical keyword.  For example, use `__asm__' instead
  832. of `asm', `__const__' instead of `const', and `__inline__' instead of
  833. `inline'.
  834.  
  835.    Other C compilers won't accept these alternative keywords; if you
  836. want to compile with another compiler, you can define the alternate
  837. keywords as macros to replace them with the customary keywords.  It
  838. looks like this:
  839.  
  840.      #ifndef __GNUC__
  841.      #define __asm__ asm
  842.      #endif
  843.  
  844.    `-pedantic' causes warnings for many GNU C extensions.  You can
  845. prevent such warnings within one expression by writing `__extension__'
  846. before the expression.  `__extension__' has no effect aside from this.
  847.  
  848. 
  849. File: gcc.info,  Node: Incomplete Enums,  Next: Function Names,  Prev: Alternate Keywords,  Up: C Extensions
  850.  
  851. Incomplete `enum' Types
  852. =======================
  853.  
  854.    You can define an `enum' tag without specifying its possible values.
  855. This results in an incomplete type, much like what you get if you write
  856. `struct foo' without describing the elements.  A later declaration
  857. which does specify the possible values completes the type.
  858.  
  859.    You can't allocate variables or storage using the type while it is
  860. incomplete.  However, you can work with pointers to that type.
  861.  
  862.    This extension may not be very useful, but it makes the handling of
  863. `enum' more consistent with the way `struct' and `union' are handled.
  864.  
  865.    This extension is not supported by GNU C++.
  866.  
  867. 
  868. File: gcc.info,  Node: Function Names,  Next: Return Address,  Prev: Incomplete Enums,  Up: C Extensions
  869.  
  870. Function Names as Strings
  871. =========================
  872.  
  873.    GNU CC predefines two string variables to be the name of the current
  874. function.  The variable `__FUNCTION__' is the name of the function as
  875. it appears in the source.  The variable `__PRETTY_FUNCTION__' is the
  876. name of the function pretty printed in a language specific fashion.
  877.  
  878.    These names are always the same in a C function, but in a C++
  879. function they may be different.  For example, this program:
  880.  
  881.      extern "C" {
  882.      extern int printf (char *, ...);
  883.      }
  884.      
  885.      class a {
  886.       public:
  887.        sub (int i)
  888.          {
  889.            printf ("__FUNCTION__ = %s\n", __FUNCTION__);
  890.            printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
  891.          }
  892.      };
  893.      
  894.      int
  895.      main (void)
  896.      {
  897.        a ax;
  898.        ax.sub (0);
  899.        return 0;
  900.      }
  901.  
  902. gives this output:
  903.  
  904.      __FUNCTION__ = sub
  905.      __PRETTY_FUNCTION__ = int  a::sub (int)
  906.  
  907.    These names are not macros: they are predefined string variables.
  908. For example, `#ifdef __FUNCTION__' does not have any special meaning
  909. inside a function, since the preprocessor does not do anything special
  910. with the identifier `__FUNCTION__'.
  911.  
  912. 
  913. File: gcc.info,  Node: Return Address,  Next: Other Builtins,  Prev: Function Names,  Up: C Extensions
  914.  
  915. Getting the Return or Frame Address of a Function
  916. =================================================
  917.  
  918.    These functions may be used to get information about the callers of a
  919. function.
  920.  
  921. `__builtin_return_address (LEVEL)'
  922.      This function returns the return address of the current function,
  923.      or of one of its callers.  The LEVEL argument is number of frames
  924.      to scan up the call stack.  A value of `0' yields the return
  925.      address of the current function, a value of `1' yields the return
  926.      address of the caller of the current function, and so forth.
  927.  
  928.      The LEVEL argument must be a constant integer.
  929.  
  930.      On some machines it may be impossible to determine the return
  931.      address of any function other than the current one; in such cases,
  932.      or when the top of the stack has been reached, this function will
  933.      return `0'.
  934.  
  935.      This function should only be used with a non-zero argument for
  936.      debugging purposes.
  937.  
  938. `__builtin_frame_address (LEVEL)'
  939.      This function is similar to `__builtin_return_address', but it
  940.      returns the address of the function frame rather than the return
  941.      address of the function.  Calling `__builtin_frame_address' with a
  942.      value of `0' yields the frame address of the current function, a
  943.      value of `1' yields the frame address of the caller of the current
  944.      function, and so forth.
  945.  
  946.      The frame is the area on the stack which holds local variables and
  947.      saved registers.  The frame address is normally the address of the
  948.      first word pushed on to the stack by the function.  However, the
  949.      exact definition depends upon the processor and the calling
  950.      convention.  If the processor has a dedicated frame pointer
  951.      register, and the function has a frame, then
  952.      `__builtin_frame_address' will return the value of the frame
  953.      pointer register.
  954.  
  955.      The caveats that apply to `__builtin_return_address' apply to this
  956.      function as well.
  957.  
  958. 
  959. File: gcc.info,  Node: Other Builtins,  Next: Deprecated Features,  Prev: Return Address,  Up: C Extensions
  960.  
  961. Other built-in functions provided by GNU CC
  962. ===========================================
  963.  
  964.    GNU CC provides a large number of built-in functions other than the
  965. ones mentioned above.  Some of these are for internal use in the
  966. processing of exceptions or variable-length argument lists and will not
  967. be documented here because they may change from time to time; we do not
  968. recommend general use of these functions.
  969.  
  970.    The remaining functions are provided for optimization purposes.
  971.  
  972.    GNU CC includes builtin versions of many of the functions in the
  973. standard C library.  These will always be treated as having the same
  974. meaning as the C library function even if you specify the
  975. `-fno-builtin' (*note C Dialect Options::.) option.  These functions
  976. correspond to the C library functions `alloca', `ffs', `abs', `fabsf',
  977. `fabs', `fabsl', `labs', `memcpy', `memcmp', `strcmp', `strcpy',
  978. `strlen', `sqrtf', `sqrt', `sqrtl', `sinf', `sin', `sinl', `cosf',
  979. `cos', and `cosl'.
  980.  
  981.    You can use the builtin function `__builtin_constant_p' to determine
  982. if a value is known to be constant at compile-time and hence that GNU
  983. CC can perform constant-folding on expressions involving that value.
  984. The argument of the function is the value to test.  The function
  985. returns the integer 1 if the argument is known to be a compile-time
  986. constant and 0 if it is not known to be a compile-time constant.  A
  987. return of 0 does not indicate that the value is *not* a constant, but
  988. merely that GNU CC cannot prove it is a constant with the specified
  989. value of the `-O' option.
  990.  
  991.    You would typically use this function in an embedded application
  992. where memory was a critical resource.  If you have some complex
  993. calculation, you may want it to be folded if it involves constants, but
  994. need to call a function if it does not.  For example:
  995.  
  996.      #define Scale_Value(X)  \
  997.        (__builtin_constant_p (X) ? ((X) * SCALE + OFFSET) : Scale (X))
  998.  
  999.    You may use this builtin function in either a macro or an inline
  1000. function.  However, if you use it in an inlined function and pass an
  1001. argument of the function as the argument to the builtin, GNU CC will
  1002. never return 1 when you call the inline function with a string constant
  1003. or constructor expression (*note Constructors::.) and will not return 1
  1004. when you pass a constant numeric value to the inline function unless you
  1005. specify the `-O' option.
  1006.  
  1007. 
  1008. File: gcc.info,  Node: Deprecated Features,  Prev: Other Builtins,  Up: C Extensions
  1009.  
  1010. Deprecated Features
  1011. ===================
  1012.  
  1013.    In the past, the GNU C++ compiler was extended to experiment with new
  1014. features, at a time when the C++ language was still evolving. Now that
  1015. the C++ standard is complete, some of those features are superceded by
  1016. superior alternatives. Using the old features might cause a warning in
  1017. some cases that the feature will be dropped in the future. In other
  1018. cases, the feature might be gone already.
  1019.  
  1020.    While the list below is not exhaustive, it documents some of the
  1021. options that are now deprecated:
  1022.  
  1023. `-fthis-is-variable'
  1024.      In early versions of C++, assignment to this could be used to
  1025.      implement application-defined memory allocation. Now, allocation
  1026.      functions (`operator new') are the standard-conforming way to
  1027.      achieve the same effect.
  1028.  
  1029. `-fexternal-templates'
  1030. `-falt-external-templates'
  1031.      These are two of the many ways for g++ to implement template
  1032.      instantiation. *Note Template Instantiation::. The C++ standard
  1033.      clearly defines how template definitions have to be organized
  1034.      across implementation units. g++ has an implicit instantiation
  1035.      mechanism that should work just fine for standard-conforming code.
  1036.  
  1037. 
  1038. File: gcc.info,  Node: C++ Extensions,  Next: Gcov,  Prev: C Extensions,  Up: Top
  1039.  
  1040. Extensions to the C++ Language
  1041. ******************************
  1042.  
  1043.    The GNU compiler provides these extensions to the C++ language (and
  1044. you can also use most of the C language extensions in your C++
  1045. programs).  If you want to write code that checks whether these
  1046. features are available, you can test for the GNU compiler the same way
  1047. as for C programs: check for a predefined macro `__GNUC__'.  You can
  1048. also use `__GNUG__' to test specifically for GNU C++ (*note Standard
  1049. Predefined Macros: (cpp.info)Standard Predefined.).
  1050.  
  1051. * Menu:
  1052.  
  1053. * Naming Results::      Giving a name to C++ function return values.
  1054. * Min and Max::        C++ Minimum and maximum operators.
  1055. * Destructors and Goto:: Goto is safe to use in C++ even when destructors
  1056.                            are needed.
  1057. * C++ Interface::       You can use a single C++ header file for both
  1058.                          declarations and definitions.
  1059. * Template Instantiation:: Methods for ensuring that exactly one copy of
  1060.                          each needed template instantiation is emitted.
  1061. * Bound member functions:: You can extract a function pointer to the
  1062.                         method denoted by a `->*' or `.*' expression.
  1063. * C++ Signatures::    You can specify abstract types to get subtype
  1064.              polymorphism independent from inheritance.
  1065.  
  1066.